package test;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.Scanner;

import javax.naming.Context;
import javax.naming.InitialContext;

import com.sun.org.apache.xml.internal.security.Init;

import jdbcinterface.GoggleDriver;
import rmi.RemoteConnection;
import rmi.RemoteDriver;
import rmi.RemoteDriverImpl;
import storage.data.StorageInterface;
import Common.GlobalConst;
import Common.GoggleDB;
import Metadata.HeapfileInterface;
import Metadata.TableMgr;
import Metadata.tableSchema;
import SQL.plan.Plan;
import SQL.planner.Planner;
import SQL.query.Select;
import SQL.scan.Scan;
import Transaction.Transaction;

//import storage.data.DataBaseFile;

public class MainServer {
	public static void main(String args[]) throws Exception {
		// new GoggleDB("test", 0);
		// // Planner planner=new Planner();
		// // planner.getExecute("CREATE DATABASE test",null);
		// //
		// planner.getExecute("CREATE TABLE conglei (AZURENAME CHAR(10),AZURENO INT,primary key(AZURENO))","test");
		// // Transaction tx=new Transaction();
		// // tableSchema record=new tableSchema();
		// // GoggleDB.tableMgr.getInfo("test.conglei", record, tx);
		// // planner.getExecute("CREATE TABLE t1 (s1 INT)","test");
		// // planner.getExecute("INSERT INTO t1 VALUES (1)","test");
		// // planner.getExecute("CREATE TABLE t2 (s1 INT)","test");
		// // planner.getExecute("INSERT INTO t2 VALUES (2)","test");
		// // planner.getExecute("CREATE TABLE t3 (s1 INT)","test");
		// // planner.getExecute("INSERT INTO t3 VALUES (2)","test");
		// //planner.getExecute("UPDATE conglei SET AZURENAME = \"AAAAA\"",
		// "test");
		// //planner.getExecute("DELETE FROM conglei WHERE AZURENO > 5",
		// "test");
		// //GoggleDB.storageInterface.get_db_by_name("test");
		// // GoggleDriver driver=new GoggleDriver();
		// Context nameContext = new InitialContext();
		// RemoteDriver d = new RemoteDriverImpl();
		// nameContext.rebind("rmi://localhost:9654/goggle", d);
		// // driver.connect("jdbc:goggle://localhost/test", null);
		// // Plan p = GoggleDB.planner.getExecute("SELECT * FROM t1","test");
		// // Scan s = p.open();
		// // s.getSchema();
		// // while(s.next()!=null){
		// // System.out.println(s.getCurrent().toString());
		// // }
		// //
		// // GoggleDB.bufferMgr.flushAll();
		// // GoggleDB.storageInterface.closeDB();
		// GoggleDB.timer.cancel();
		 Process process = null ;
		try {
			new GoggleDB("testDB", 500);
			String str = System.getProperty( "java.class.path" ) + ".;" ;
			String[] buff = new String [ System.getenv().size() ] ;
			int i = 0 ;
			for ( Map.Entry< String, String > s : System.getenv().entrySet() ) {
				if ( s.getKey().equalsIgnoreCase("classpath")) 
					buff[ i++ ] = ( s.getKey() +  "=" + str ) ;
				else buff [ i++ ] = ( s.getKey() +  "=" + s.getValue() ) ; 
			}
			process = Runtime.getRuntime().exec( "rmiregistry", buff ) ;
			Thread.sleep( 1000 );
			RemoteDriver driver = new RemoteDriverImpl();
			Naming.rebind("rmi://localhost/goggle", driver);
			System.out.println("Server Started Succesfully");
			// process.destroy() ;
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
